package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Log;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.TabEntity;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.migu.contacts.R;
import com.migu.contacts.bean.BirthdayWordsBean;
import com.migu.contacts.databinding.ActivityBirthdayWordsBinding;
import com.migu.contacts.databinding.ItemBirthdaywordsBinding;

import java.util.ArrayList;
import java.util.List;

public class BirthdayWordsActivity extends BaseActivity<ActivityBirthdayWordsBinding> {
    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemBirthdaywordsBinding, BirthdayWordsBean> adapter = new BaseAdapter<ItemBirthdaywordsBinding, BirthdayWordsBean>() {
        @OnClick(R.id.confirm)
        public void confirm(BirthdayWordsBean bean) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", bean.name);
            clipboard.setPrimaryClip(clip);
            finish();
        }
    };

    @Override

    protected void initData() {
        super.initData();
        ArrayList<CustomTabEntity> list = new ArrayList<>();
        list.add(new TabEntity("全部", 0, 0));
        list.add(new TabEntity("幽默类", 0, 0));
        list.add(new TabEntity("经典类", 0, 0));
        list.add(new TabEntity("文艺类", 0, 0));
        mBinding.tabLayout.setTabData(list);

        mBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                Log.d(TAG, "onTabSelect() called with: position = [" + position + "]");
                select(position);

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        select(0);
    }

    private void select(int position) {
        API.getBirthdayWords(position).watch(mActivity, new HttpCallback<List<BirthdayWordsBean>>() {
            @Override
            protected void onSuccess(List<BirthdayWordsBean> ossToken) {
                if (ossToken != null && ossToken.size() > 0) {
                    ossToken.get(0).isFirst = true;
                }
                adapter.setData(ossToken);
            }
        });
    }
}